package engine.newUi
{
	import com.greensock.TweenLite;
	import com.greensock.plugins.VolumePlugin;
	
	import engine.control.loading.MultLoader;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.logic.AlertLogic;
	import engine.logic.ClearCDTimeLogic;
	import engine.logic.login.PlayCDTimerLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.CDClearTypeUI;
	import engine.ui.MsgTipsLogic;
	import engine.util.FilterUtil;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;
	

	public class PulseLogic
	{
		private static var _self : PulseLogic;
		private var _ui : PulseUI;
		private var _model : ModelLocator;
		private var _bindManage:BindManage;
		private var isBind : Boolean;
		/**所有经脉点影片剪辑*/
		private var pointMCArr : Array;
		
		private var currentPulseID : int;
		/**所有经脉*/
		private var allPulseArray:Array = [];
		
		private var _cdTime : int;
		
		public function PulseLogic()
		{
		}
		public static function getInstance():PulseLogic
		{
			if(!_self)
			{
				_self = new PulseLogic();
			}
			return _self;
		}
		public function Init():void
		{
			_model = ModelLocator.getInstance();
			_bindManage = BindManage.getInstance();
			//成功打通经脉穴位
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_MERIDIAN_GRADE_SCCUEESS),addPulse);
		}
		
		/**
		 *成功打通经脉  勾选了自动清空CD ，会自动请求CD清空 
		 * @param e
		 * 
		 */
		private function addPulse(e:CustomMsgEvent):void
		{
			MsgTipsLogic.getRes(_ui.getPointBtn).showInfo(StaticIni.getSwfLangStr("Pulse_Str8"));
		}
		
		/**
		 *经脉是否可点 
		 * @return 
		 * 
		 */
		public function checkIsCanStudy():Boolean
		{
			
			return false;
		}
		
		public function BeforeShow():void
		{
			ui.visible = false;
			LoadUI();
		}
		
		public function AfterHide():void
		{
			removeListenter();
		}
		
		private function LoadUI():void
		{
			//是否加载资源
			if(_ui.skill_mc.numChildren == 0)
			{
				MultLoader.getInstance().load(
					[ResPath.SWF+"pulse.swf"],true,CreateUI);
			}
			else
			{
				addListenter();
			}
		}
		
		/**
		 *添加监听 
		 * 
		 */
		private function addListenter():void
		{
			ui.visible = true;
			ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onOutClickHandle);
			ui.select.addEventListener(MouseEvent.CLICK,onSelectClick);
			ui.powerClip.addEventListener(MouseEvent.ROLL_OVER,onPower);
			ui.powerClip.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			ui.getPointBtn.addEventListener(MouseEvent.CLICK,onGetPointClick);
			ui.select.addEventListener(MouseEvent.ROLL_OVER,overClearCDBtn);
			ui.select.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			ui.autoTime.addEventListener(MouseEvent.ROLL_OVER,overClearCDBtn);
			ui.autoTime.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			ui.clearCDBtn.addEventListener(MouseEvent.CLICK,onClickClearCD);
			ui.clearCDBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			ui.clearCDBtn.addEventListener(MouseEvent.ROLL_OVER,overClearCDBtn);
			for(var i:int;i<pointMCArr.length;i++)
			{
				(pointMCArr[i] as MovieClip).addEventListener(MouseEvent.CLICK,onMouseClick);
				(pointMCArr[i] as MovieClip).addEventListener(MouseEvent.ROLL_OVER,onMouseOver);
				(pointMCArr[i] as MovieClip).addEventListener(MouseEvent.ROLL_OUT,hideTips);
			}
		}
		
		/**
		 *移除监听 
		 * 
		 */
		private  function removeListenter():void
		{
			ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onOutClickHandle);
			ui.select.removeEventListener(MouseEvent.CLICK,onSelectClick);
			ui.powerClip.removeEventListener(MouseEvent.ROLL_OVER,onPower);
			ui.powerClip.removeEventListener(MouseEvent.ROLL_OUT,hideTips);
			ui.getPointBtn.removeEventListener(MouseEvent.CLICK,onGetPointClick);
			ui.select.removeEventListener(MouseEvent.ROLL_OVER,overClearCDBtn);
			ui.select.removeEventListener(MouseEvent.ROLL_OUT,hideTips);
			ui.autoTime.removeEventListener(MouseEvent.ROLL_OVER,overClearCDBtn);
			ui.autoTime.removeEventListener(MouseEvent.ROLL_OUT,hideTips);
			ui.clearCDBtn.removeEventListener(MouseEvent.CLICK,onClickClearCD);
			ui.clearCDBtn.removeEventListener(MouseEvent.ROLL_OUT,hideTips);
			ui.clearCDBtn.removeEventListener(MouseEvent.ROLL_OVER,overClearCDBtn);
			for(var i:int;i<pointMCArr.length;i++)
			{
				(pointMCArr[i] as MovieClip).removeEventListener(MouseEvent.CLICK,onMouseClick);
				(pointMCArr[i] as MovieClip).removeEventListener(MouseEvent.ROLL_OVER,onMouseOver);
				(pointMCArr[i] as MovieClip).removeEventListener(MouseEvent.ROLL_OUT,hideTips);
			}
		}
		
		private function CreateUI():void
		{
			var uiMC : MovieClip = MultLoader.getInstance().getSourceByUrl(ResPath.SWF+"pulse.swf")
			_ui.skill_mc.addChild(uiMC);
			pointMCArr = [uiMC["pulse_1"],
				uiMC["pulse_2"],
				uiMC["pulse_3"],
				uiMC["pulse_4"],
				uiMC["pulse_5"],
				uiMC["pulse_6"],
				uiMC["pulse_7"],
				uiMC["pulse_8"]];
			ui.select = uiMC["select"];
			ui.autoTime = uiMC["autoTime"];
			ui.select.buttonMode = true;
			ui.clearCDBtn.visible = false;
			ui.select.gotoAndStop(1) ;
			
			_ui.needLab.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.timeLab.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.nextProLab.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.currentProLab.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.currentProTxt.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.currentTxt.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.needTxt.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.nextProTxt.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.powerLab.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.timeTxt.txtFilters = [FilterUtil.WhiteColorFilter];
//			_ui.autoRestTimeLab.txtFilters = [FilterUtil.WhiteColorFilter];
			_ui.helpTxt.setFilters([FilterUtil.WhiteColorFilter]);
			_ui.helpTip.txtFilters = [FilterUtil.WhiteColorFilter];
			
			_ui.helpTxt.htmlText = "<font color='#462d0a' size='13'><b>"+StaticIni.getSwfLangStr("Pulse_Str7")+"</b></font>";
//			_ui.autoRestTimeLab.htmlText = "<font color='#009900' size='14'><b>"+StaticIni.getSwfLangStr("Pulse_Str6")+"</b></font>";
			_ui.timeLab.htmlText = "<font color='#462d0a' size='13'><b>"+StaticIni.getSwfLangStr("Pulse_Str5")+"</b></font>";
			_ui.needLab.htmlText = "<font color='#462d0a' size='13'><b>"+StaticIni.getSwfLangStr("Pulse_Str4")+"</b></font>";
			_ui.nextProLab.htmlText = "<font color='#462d0a' size='13'><b>"+StaticIni.getSwfLangStr("Pulse_Str3")+"</b></font>";
			_ui.currentProLab.htmlText = "<font color='#462d0a' size='13'><b>"+StaticIni.getSwfLangStr("Pulse_Str2")+"</b></font>";
			_ui.helpTip.htmlText = "<font color='#5d0202' size='14'><b>"+StaticIni.getSwfLangStr("UIString_1000737")+"</b></font>";
			
			for(var i:int=0;i<pointMCArr.length;i++)
			{
				pointMCArr[i].gotoAndStop(1);
				pointMCArr[i].buttonMode = true;
			}
			
			PlayCDTimerLogic.getInstance().resgis(21,function (val:int,col:int=0):void
			{
				_cdTime = val;
				ui.timeTxt.htmlText	=	"<font color='#5d0202' size='13'><b>"+PlayCDTimerLogic.getInstance().getTimerStr(val,true)+"</b></font>";
				ui.clearCDBtn.visible = (_cdTime>0);
				
			});
			
			if(!isBind)
			{
				isBind = true;
				//当前经脉
				_bindManage.BindProp(_model.scene.Role,"CurMeridianID",this,"CurMeridianID");
				//所有经脉战斗力加成
				_bindManage.BindProp(_model.scene.Role,"CurMeridianFightPower",this,"CurMeridianFightPower");
				//经脉表
				_bindManage.BindRecord(_model.scene,"NewMeridianLevelRec",pulseRecAdd,pulseRecremove);
			}
			addListenter();
		}
		
		
		public function set CurMeridianFightPower(val : int):void
		{
			ui.powerLab.htmlText = "<font color='#5d0202' size='14'><b>"+val+"</b></font>";
		}
		
		public function set CurMeridianID(val : int):void
		{
			currentPulseID = val;
			if(allPulseArray.length == 8)
			{
				setPoint();
				setLab();
			}
		}
		
		private function onClickClearCD(e:MouseEvent):void
		{
			if(_cdTime>0)
			{
				SitAlerLogic.getIntatic().setUIinfo(StaticIni.getSwfLangStrVar("UIString_1000713", [getNeedMagicCoin(_cdTime)]),
				[],
				function():void{SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_REQUEST_CLEAR_CD,21);},
				ui.clearCDBtn);
			}
		}
		
		private function pulseRecAdd(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			var obj : Object = new Object();
			obj["MeridianID"] = PropertiesValue[0];
			obj["ProName"] = PropertiesValue[1];
			obj["ProValue"] = PropertiesValue[2];
			obj["Level"] = PropertiesValue[3];
			allPulseArray[int(PropertiesName)] = obj;
			if(allPulseArray.length == 8)
			{
				setPoint();
				setLab();
			}
		}
		
		private function pulseRecremove(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			
		}
		
		private function setPoint():void
		{
			for(var i:int = 0;i<pointMCArr.length;i++)
			{
				var PointMc : MovieClip = pointMCArr[i] as MovieClip;
				var obj : Object = allPulseArray[i];
				PointMc["MeridianID"] = obj["MeridianID"];
				PointMc["ProName"] = obj["ProName"];
				PointMc["ProValue"] = obj["ProValue"];
				PointMc["Level"] = obj["Level"];
				//升级需要的经脉等级前提
				if(PointMc["MeridianID"] == allPulseArray[0]["MeridianID"])
				{
					PointMc["needLevel"] = PointMc["Level"];
				}
				else
				{
					PointMc["needLevel"] = PointMc["Level"] + 1;
				}

				//升级需要的经脉名字前提
				if(i>0)
				{
					PointMc["needValue"] = allPulseArray[i-1]["MeridianID"];
				}
				else
				{
					PointMc["needValue"] = allPulseArray[allPulseArray.length-1]["MeridianID"];
				}
				if(PointMc["MeridianID"] == allPulseArray[0]["MeridianID"] && PointMc["Level"] == 0)
				{
					PointMc["needValue"] = "";
					PointMc["needLevel"] = "";
				}
				
				
				for(var j:int =0 ;j<PointMc.numChildren;j++)
				{
					if(PointMc.getChildAt(j) is NxClip)
					{
						PointMc.removeChildAt(j);
					}
						
				}
				
				//当前要打通的经脉
				if(int(PointMc["MeridianID"]) == currentPulseID)
				{
					var effectClip : NxClip = new NxClip();
					effectClip.source = ResManager.newResClass("custSkin_18");
					effectClip.x = 10;
					effectClip.y = 10;
					PointMc.addChild(effectClip);
				}
				setPointType(PointMc);
			}
		}
		
		
		/**
		 *设置lab的文本内容 
		 * 
		 */
		private function setLab():void
		{
			var pulseName : String;
			var pulsePro : String;
			var nextPulsePro :String;
			var needPulseName : String;
			
			for(var i:int = 0;i<pointMCArr.length;i++)
			{
				var PointMc : MovieClip = pointMCArr[i] as MovieClip;
				PointMc["LevelTxt"].htmlText = PointMc["Level"];
				if(int(PointMc["MeridianID"]) == currentPulseID)
				{
					pulseName = StaticIni.getSwfLangStrVar("Pulse_Str1",[StaticIni.getSwfLangStr("Pulse_"+PointMc["MeridianID"]),PointMc["Level"]]);
					pulsePro = StaticIni.getSwfLangStrVar("Item_"+PointMc["ProName"],[PointMc["ProValue"]]);
					nextPulsePro = StaticIni.getSwfLangStrVar("Item_"+PointMc["ProName"],[PointMc["ProValue"]+getProValue(PointMc["MeridianID"],PointMc["Level"])]);
					if(PointMc["MeridianID"] == allPulseArray[0]["MeridianID"] && PointMc["Level"] == 0)
					{
						needPulseName = "";
					}
					else 
					{
						needPulseName = StaticIni.getSwfLangStrVar("Pulse_Str9",[StaticIni.getSwfLangStr("Pulse_"+PointMc["needValue"]),PointMc["needLevel"]]);
					}
				}
			}
			ui.currentTxt.htmlText = "<font color='#462d0a' size='14'><b>"+pulseName+"</b></font>";
			ui.currentProTxt.htmlText = "<font color='#5d0202' size='13'><b>"+pulsePro+"</b></font>";
			ui.nextProTxt.htmlText = "<font color='#5d0202' size='13'><b>"+nextPulsePro+"</b></font>";
			ui.needTxt.htmlText = "<font color='#5d0202' size='13'><b>"+needPulseName+"</b></font>";
		}
		
		/**
		 *本地配置文件配置的 每个等级加的不同属性值 
		 * @param pulseID
		 * @return 
		 * 
		 */
		private function getProValue(pulseID : String , pulseLevel:int):int
		{
			var level : String;
			//判断等级取值范围
//			if(pulseLevel>90)
//			{
//				return 0;
//			}
			if(pulseLevel>=75)
			{
				level = "90";
			}
			else if(pulseLevel>=60)
			{
				level = "75";
			}
			else if(pulseLevel>=40)
			{
				level = "60";
			}
			else if(pulseLevel>=20)
			{
				level = "40";
			}
			else if(pulseLevel>=0)
			{
				level = "20";
			}
			var val : int =  int(StaticIni.getIniVar(pulseID,level))
			return val;
		}
		
		/**
		 *设置经脉点的 颜色等级 
		 * @param pointMc
		 * 
		 */
		private function setPointType(pointMc:MovieClip):void
		{
			if(pointMc["Level"] == 0)
			{
				pointMc.gotoAndStop(1);
			}
			else
			{
				pointMc.gotoAndStop(2);
			}
		}
		
		/**
		 *点击打通经脉按钮 
		 * @param e
		 * 
		 */
		private function onGetPointClick(e:MouseEvent):void
		{
			if(ui.select.currentFrame == 2)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_REQUEST_CLEAR_CD,21);
			}
			sendGetPoint();
		}
		
		/**
		 *清除CDTips
		 * @param e
		 * 
		 */		
		private function overClearCDBtn(e:MouseEvent):void
		{
			TipsMainLogic.instance.showMain(StaticIni.getSwfLangStrVar("UIString_1000713", [getNeedMagicCoin(_cdTime)]));
		}
		
		/**
		 *自动清除CD时间 
		 * @param e
		 * 
		 */
		private function onSelectClick(e:MouseEvent):void
		{
			if(ui.select.currentFrame == 1)
			{
				ui.select.gotoAndStop(2);
			}
			else
			{
				ui.select.gotoAndStop(1);
			}
		}
		
		private function sendGetPoint():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_UPGRADE_MERIDIAN);
		}
		
		/**
		 *点击经脉点 
		 * @param e
		 * 
		 */
		private function onMouseClick(e:MouseEvent):void
		{
			var pointMc : MovieClip = e.currentTarget as MovieClip;
			
			if(int(pointMc["MeridianID"]) == currentPulseID)
			{
				if(ui.select.currentFrame == 2)
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_REQUEST_CLEAR_CD,21);
				}
				sendGetPoint();
			}
		}
		
		/**
		 *计算出清除CD所需的元宝 
		 * @param val
		 * @return 
		 * 
		 */
		private function getNeedMagicCoin(val : int):String
		{
			var minutes :Number;
			var seconds:Number;
			var needStr:String	=	"";
			minutes		=	Math.floor(val / 60);
			seconds		=	val  - minutes * 60;
			if (seconds > 0) 
			{
				minutes++;
			}
			needStr	=	String(1 * minutes);
			return needStr;
		}
		
		
		/**
		 * 战斗力加成Tips
		 * @param e
		 * 
		 */
		private function onPower(e:MouseEvent):void
		{
			var tips : String = StaticIni.getSwfLangStr("UIString_1000709");
			for(var i :int;i<pointMCArr.length;i++)
			{
				var pointMc : MovieClip = pointMCArr[i];
				if(int(pointMc["ProValue"])>0)
				{
					var str : String = StaticIni.getSwfLangStrVar("Item_"+pointMc["ProName"],[pointMc["ProValue"]]);
					tips = tips+"<br><font color='#cc6600' size='13'>"+str+"</font>";
				}
			}
			if(pointMCArr[0]["Level"] == 0)
			{
				tips = tips+"<br>"+StaticIni.getSwfLangStr("UIString_1000647");
			}
			TipsMainLogic.instance.showMain(tips);
		}
		
		/**
		 *经脉tips显示 
		 * @param e
		 * 
		 */
		private function onMouseOver(e:MouseEvent):void
		{
			var pointMc : MovieClip = e.currentTarget as MovieClip;
			var str1 : String = StaticIni.getSwfLangStrVar("Item_"+pointMc["ProName"],[pointMc["ProValue"]]);
			var str2 : String = pointMc["Level"];
			var str3 : String = StaticIni.getSwfLangStrVar("Pulse_Str9",[StaticIni.getSwfLangStr("Pulse_"+pointMc["needValue"]),pointMc["needLevel"]]);
			if(pointMc["MeridianID"] == allPulseArray[0]["MeridianID"] && pointMc["Level"] == 0)
			{
				str3 = StaticIni.getSwfLangStr("UIString_1000647");
			}
			TipsMainLogic.instance.showMain(StaticIni.getSwfLangStrVar("Pulse_Str10",[str1,str2,str3]));
		}
		
		/**
		 *tips隐藏 
		 * @param e
		 * 
		 */
		private function hideTips(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		/**
		 *关闭 
		 * @param e
		 * 
		 */
		private function onOutClickHandle(e:NxWindowEvent):void
		{
			ui.hide();	
		}
		
		public function get ui():PulseUI
		{
			if(!_ui)
			{
				_ui	=new PulseUI();
			}
			return _ui
		}
	}
}